/**
 * 宁夏省政府采购网爬虫
 *  
 */
import {Base,Item} from '../base';
class nxszfcg extends Base{
    document:Document;
    constructor(){
        super();
        this.name = "宁夏省政府采购网";
        this.enabled = true;
        this.encode = 'utf-8';
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName = "招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex:number):Item[]{
        let url=`http://www.ccgp-ningxia.gov.cn/public/NXGPP/dynamic/contents/CGGG/ZBGG/index.jsp?cid=316&sid=1&page=${pageIndex}`;
        let result :Item[]= [];
        let content=this.spider.getSync(url);
        let $=this.cheerio.load(content);
        let that =this;
        $('body table table').find('tr').each(function (index, ele) {
            let item =that.buildItem();
            item.title=$(ele).find('td').eq(0).find('a').text().trim();
            item.source=that.name;
            (<any>item)['公告时间']=$(ele).find('td').eq(1).text().trim();
            let uri='http://www.nxgp.gov.cn/public/NXGPP/dynamic/'+$(ele).find('td').eq(0).find('a').attr('href');
            (<any>item).url='http://www.nxgp.gov.cn/public/NXGPP/dynamic/'+$(ele).find('td').eq(0).find('a').attr('href');
            result=that.getExtraItem(uri,item,result);
        });
        return result;
    }
    getExtraItem(url:string, item:Item, result:Item[]) {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        (<any>item).content =$('table tr').eq(5).html(); 
        result.push(item);
        return result;
    }
    getPageCount() {
        return 20;
    }
}
module.exports=new nxszfcg();